﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCodeCreater.Module
{
    public class CreateModel
    {
        public static void Create(DataTable table, TableInfo tableinfo, string root, 
            string modelContent,string createInputContent,string updateInputContent,string listInputContent,string listOutContent)
        {
            modelContent = modelContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            createInputContent = createInputContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            updateInputContent = updateInputContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            listInputContent = listInputContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            listOutContent = listOutContent.Replace("_tabledesc_", tableinfo.value).Replace("_name_", tableinfo.name);
            CreateEntity(table, root, modelContent, tableinfo, new string[] { "ID", "CreateTime", "IsDeleted" });
            CreateCreateInput(table, root + $@"\Domain\Dto\{tableinfo.name}\{tableinfo.name}CreateInput.cs", createInputContent, tableinfo, new string[] { "ID", "CreateTime", "IsDeleted" });
            CreateCreateInput(table, root + $@"\Domain\Dto\{tableinfo.name}\{tableinfo.name}UpdateInput.cs", updateInputContent, tableinfo, new string[] { "CreateTime", "IsDeleted" });

            CreateCreateInput(table, root + $@"\Domain\Dto\{tableinfo.name}\{tableinfo.name}ListDto.cs", listOutContent, tableinfo, new string[] { "CreateTime", "IsDeleted" });
            CreateCreateInput(table, root + $@"\Domain\Dto\{tableinfo.name}\{tableinfo.name}ListInput.cs", listInputContent, tableinfo, new string[] { },true);
        }

        static void CreateEntity(DataTable table, string root, string modelContent, TableInfo tableinfo, string[] replace)
        {
            var mdforeach = new StringBuilder();
            var setValueEach = new StringBuilder();
            foreach (DataRow dataRow in table.Rows)
            {
                mdforeach.Append("        /// <summary>\r\n");
                mdforeach.Append("        /// " + dataRow[5] + "\r\n");
                mdforeach.Append("        /// </summary>\r\n");
                mdforeach.Append("        [DisplayName(@\"" + dataRow[5] + "\")]\r\n");
                if (dataRow[6].ToString() == "1")
                    mdforeach.Append("        [Key]\r\n");
                mdforeach.Append(string.Concat(new object[]
                {
                    "        public ",
                    Tool.GetCsharpTypeInSql(dataRow[1].ToString(),dataRow[2].ToString(),out Type type),
                    " ",
                    dataRow[0],
                    " { get; set; }\r\n\r\n"
                }));
                if (!replace.Contains(dataRow[0]))
                {
                    setValueEach.AppendLine("            " + dataRow[0] + " = " + "input." + dataRow[0] + ";");
                }
            }
            modelContent = modelContent.Replace("_foreach_", mdforeach.ToString()).Replace("_SetValueEach_", setValueEach.ToString());
            Tool.SaveFile(root + $@"\Domain\Model\{tableinfo.name}.cs", modelContent);

        }

        static void CreateCreateInput(DataTable table, string root, string modelContent, TableInfo tableinfo,
            string[] replace, bool defaultnull = false)
        {
            var createforeach = new StringBuilder();
            foreach (DataRow dataRow in table.Rows)
            {
                if (!replace.Contains(dataRow[0]))
                {
                    var type = Tool.GetCsharpTypeInSql(dataRow[1].ToString(), dataRow[2].ToString(), out Type strType);
                    createforeach.Append(string.Concat(new object[]
                    {
                        "        public ",
                        type,
                        defaultnull && (type!="string") && (string) dataRow[0]!="IsDeleted" ? "? ":" ",
                        dataRow[0],
                        " { get; set; }\r\n\r\n"
                    }));
                }
            }
            modelContent = modelContent.Replace("_foreach_", createforeach.ToString());
            Tool.SaveFile(root, modelContent);
        }
    }
}
